<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>闭包计算打车费用</title>
		<script type="text/javascript">
			//闭包计算打车费用
			var car = (function() {
				//起步价
				var start = 13;
				//总价
				var total = 0;
				
				return {
					price:function(distance) {
						//判断是否超出起步价
						if(distance <= 3) {
							//不超出三公里
							total = 13;
						} else {
							total = start + (distance - 3) * 5;
						}
						return total;
					},
					jarPrice:function(flag) {
						return flag ? total + 10 : total;
					}
				}
			})();
			//不拥堵
			console.log(car.price(5));
			//拥堵
			console.log(car.jarPrice(true));
		</script>
	</head>
	<body>
	</body>
</html>
